<!DOCTYPE html>
<!-- 网页使用的语言 -->
<html lang="zh-CN">

<head>
    <!-- 指定字符集 -->
    <meta charset="utf-8">
    <!-- 使用Edge最新的浏览器的渲染方式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- viewport视口：网页可以根据设置的宽度自动进行适配，在浏览器的内部虚拟一个容器，容器的宽度与设备的宽度相同。
    width: 默认宽度与设备的宽度相同
    initial-scale: 初始的缩放比，为1:1 -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <meta name="viewport"
        content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />

    <title>在线音乐服务器实现</title>

    <link rel="shortcut icon" href="#" />

    <!-- 1. 导入CSS的全局样式 -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery导入，建议使用1.9以上的版本 -->
    <script src="js/jquery-3.1.1.min.js"></script>
    <!-- 3. 导入bootstrap的js文件 -->
    <!-- <script src="js/bootstrap.min.js"></script>-->

    <!-- 引入登录校验的 JavaScript 文件 -->
    <script>
        $(document).ready(function () {
            $(document).ajaxError(function (event, xhr, settings, error) {
                if (xhr.status === 401) {
                    alert("您尚未登录，请先登录！");
                    window.location.href = "/login.html"; // 跳转到登录页面的URL
                }
            });
        });
    </script>

    <style type="text/css">
        td,
        th {
            text-align: center;
            color: crimson;
        }

        h1,
        .l1 {
            color: crimson;
        }

        body {
            background: url(images/bg2.png) no-repeat center center;
            /*background-repeat: repeat-x;*/
            background-size: cover;
        }
        html, body {
            height: 100%;
            margin: 0;
        }

        body {
            background: url(images/bg2.png) no-repeat center center;
            background-size: cover;
            min-height: 100vh; /* 确保 body 至少占据整个视口高度 */
        }

        audio{
            height: 40px;
        }
    </style>
    <script>
        $(function () {
            load();
        });

        function load(musicName) {
            $.ajax({
                url: "/loveMusic/findMyLove",
                type: "get",
                data: { musicName: musicName },
                dataType: "json",
                success: function (obj) {
                    var data = obj.data;
                    console.log(data.length);
                    console.log(data);
                    var s = '';
                    for (var i = 0; i < data.length; i++) {
                        var musicUrl = data[i].url;
                        console.log(musicUrl);
                        s += '<tr>';
                        s += '<th > <input id= "' + data[i].id + '" type="checkbox"> </th>';
                        s += '<td>' + data[i].title + data[i].type + '</td>';
                        s += '<td>' + data[i].singer + '</td>';
                        s += '<td> <button class = "btn btn-primary"  onclick="playerSong(\'' + musicUrl + '\')"> 播放歌曲 </button>' + '</td>';
                        s += '<td> <button class = "btn btn-primary"  onclick="deleteInfo(' + data[i].id + ')"> 删除 </button>' + '</td>';
                        s += "</tr>";
                    }
                    $("#info").html(s);
                }
            });
        }
        /* 播放 */
        function playerSong(obj) {
            $.ajax({
                type: "get",
                url: obj,
                dataType: "json",
                success: function (resp) {
                    console.log(resp);
                    // Base64 解码
                    var musicData = atob(resp.data);
                    var uint8Array = new Uint8Array(musicData.length);
                    for (var i = 0; i < musicData.length; i++) { uint8Array[i] = musicData.charCodeAt(i); }
                    // 创建 Blob 对象
                    var blob = new Blob([uint8Array], { type: 'audio/mpeg' });
                    var musicUrl = URL.createObjectURL(blob);
                    // 播放音乐
                    audio = document.getElementById('musicPlayer');
                    audio.src = musicUrl;
                    audio.play();
                },
                error: function (xhr, status, error) {
                    console.error("Failed to load music:", error);
                }
            });
        }

        // 移除
        function deleteInfo(obj) {
            console.log(obj);
            $.ajax({
                url: "/loveMusic/deleteMyLove",
                type: "post",
                // 发送给后端的数据
                data: { "musicId": obj },
                dataType: "json",
                success: function (obj) {
                    if (obj.status === 0) {
                        alert("移除成功");
                        window.location.href = "loveMusic.html";
                    } else {
                        alert("移除失败");
                    }
                }
            });
        }

        // 查询
        $(function () {
            $("#submit1").click(function () {
                var name = $("#exampleInputName2").val();
                load(name);
            });
        });
    </script>

</head>

<body>
    <div class="container">
        <h1 style="text-align: center">我喜欢的音乐列表</h3>
            <div style="float: left;">
                <form class="form-inline">
                    <div class="form-group">
                        <label for="exampleInputName2" class="l1">歌曲名</label>
                        <input name="name" type="text" class="form-control" id="exampleInputName2">
                    </div>
                    <button id="submit1" type="button" class="btn btn-default">查询</button>
                </form>
            </div>
            <div style="float: right;margin-bottom: 15px">
                <audio controls id="musicPlayer">
                    Your browser does not support the audio element.
                </audio>
                <a class="btn btn-primary" href="list.html">回到首页</a>
            </div>
            <table border="1" class="table table-bordered table-hover">
                <tr class="success">
                    <th>歌名</th>

                    <th>歌手</th>
                    <th>歌曲</th>
                    <th>操作</th>
                </tr>
                <tbody id="info" valign="middle">

                    <!-- <tr>
            <td>成都</td>
            <td>赵雷</td>
            <td><button class="btn btn-primary" onclick="playerSong('+musicUrl+')">播放歌曲</button></td>
            <td><button class="btn btn-primary" onclick="deleteInfo('+musicUrl+')">删除</button></td>
        </tr> -->

                </tbody>
            </table>
    </div>
</body>

</html>